<template>
  <div class="pet-category">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>宠物分类</span>
          <el-button type="primary" @click="handleAdd">添加分类</el-button>
        </div>
      </template>

      <!-- 分类列表 -->
      <el-table :data="categoryList" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="分类名称" />
        <el-table-column prop="count" label="宠物数量" width="100" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button 
              type="danger" 
              size="small" 
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑分类对话框 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form :model="categoryForm" :rules="rules" ref="categoryFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="categoryForm.name" />
        </el-form-item>
        <el-form-item label="分类描述" prop="description">
          <el-input type="textarea" v-model="categoryForm.description" rows="3" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 分类列表数据
const categoryList = ref([
  {
    id: 1,
    name: '猫咪',
    count: 25,
    createTime: '2024-03-18 10:00:00'
  },
  {
    id: 2,
    name: '狗狗',
    count: 30,
    createTime: '2024-03-18 10:00:00'
  },
  {
    id: 3,
    name: '兔子',
    count: 10,
    createTime: '2024-03-18 10:00:00'
  },
  {
    id: 4,
    name: '小鸟',
    count: 15,
    createTime: '2024-03-18 10:00:00'
  }
])

// 对话框相关
const dialogVisible = ref(false)
const dialogTitle = ref('添加分类')
const categoryFormRef = ref(null)

const categoryForm = reactive({
  name: '',
  description: ''
})

const rules = {
  name: [
    { required: true, message: '请输入分类名称', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ]
}

// 方法定义
const handleAdd = () => {
  dialogTitle.value = '添加分类'
  categoryForm.name = ''
  categoryForm.description = ''
  dialogVisible.value = true
}

const handleEdit = (row) => {
  dialogTitle.value = '编辑分类'
  Object.assign(categoryForm, row)
  dialogVisible.value = true
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该分类吗？删除后该分类下的宠物将无分类', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
  })
}

const submitForm = async () => {
  if (!categoryFormRef.value) return
  
  await categoryFormRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success(dialogTitle.value === '添加分类' ? '添加成功' : '更新成功')
      dialogVisible.value = false
    }
  })
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 